Lists
Table of Contents
Lists are built up from cons cells, which is a data object consisting of two slots, each refering to some Lisp object. One slot is car (which stores the object in this cons cell), the other is cdr refers to nil, or another cons cell in the context of lists. A list is a series of cons cells chained together.
- Proper lists
- Proper lists refer to the lists whose last cons cell’s
cdrisnil. - Dotted lists
- Dotted lists refer to the lists whose last cons cell’s
cdris notnil. - Circular lists
- … whose last cons cell’s
cdrpoint to one of the previous cons cell in the list.
1. Predicates on Lists
consp obj- Checks if the object is cons cell.
atom obj- Opposite of
consp, equivalent to(not (consp obj)) listp obj- Checks if is a cons cell or
nil. null obj- Checks if is
nil. proper-list-p obj- Check if is a proper list.
2. Accessing Elements of Lists
car cons-cell- Return the value of
carof cons cell. Or the first element in the list. cdr cons-cell- Return the
cdrof cons cell. Or the list except the first element. pop list- Removes and returns the first element of the list. It modifies the list.
nth n list- Returns the
n-th element of the list (0-indexed). Returnsnilif exceeds list length. nthcdr n list- Discards the first
nelements and returns the rest of the list. take n list- Takes the first
nelements of the list. Does not modify the list. ntake n list- Same as
takebut modifies the list.